package com.enwei.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;

import com.enwei.activitys.ShoppingCartActivity;
import com.enwei.model.ProducInfoModel;
import com.ew.action.R;

public class ShoppingCartAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<ProducInfoModel> arrays = null;
	private Context mContext;
	private ShoppingCartActivity mActivity;
	private boolean isCheck = true;
	public ShoppingCartAdapter(List<ProducInfoModel> arrays, Context context) {
		this.arrays = arrays;
		this.mContext = context;
		mActivity = (ShoppingCartActivity) context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.shopping_cart_info_cell,
					null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		onBindViewHolder(holder, position);
		return convertView;
	}

	@Override
	public int getCount() {
		return arrays!=null?arrays.size():0;
	}

	@Override
	public Object getItem(int i) {
		return arrays.get(i);
	}

	@Override
	public long getItemId(int i) {
		return 0;
	}

	private class ViewHolder {
		public TextView title, price,price1,type;
		public TextView addition,reduction,sumValue;
		public CheckBox checkbox;
		public ViewHolder(View view) {
			title = (TextView) view.findViewById(R.id.title);
			price = (TextView) view.findViewById(R.id.price);
			price1 = (TextView) view.findViewById(R.id.price1);
			type = (TextView) view.findViewById(R.id.type);
			addition = (TextView) view.findViewById(R.id.addition);
			reduction = (TextView) view.findViewById(R.id.reduction);
			sumValue = (TextView) view.findViewById(R.id.sumValue);
			checkbox = (CheckBox) view.findViewById(R.id.checkbox);
		}
	}

	private void onBindViewHolder(final ViewHolder holder, int position) {
		
		final ProducInfoModel model = (ProducInfoModel) getItem(position);
		if(isCheck)holder.checkbox.setChecked(true);
		else holder.checkbox.setChecked(false);
		holder.title.setText(model.getTitle());
		holder.price.setText("¥"+model.getPrice());
		holder.type.setText(model.getProductType());
		double sumPrice = Double.valueOf(model.getPrice())*model.getBuyCount();
		holder.price1.setText("¥"+sumPrice);
		holder.sumValue.setText(model.getBuyCount()+"");
		holder.addition.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				try {
					model.setBuyCount((model.getBuyCount()+1));
					mActivity.dbUtils.update(model);
					
					int sum = Integer.valueOf(holder.sumValue.getText().toString());
					sum++;
					holder.sumValue.setText(sum+"");
					mActivity.initData();
				} catch (Exception e) {
					e.printStackTrace();
				}
//				double price = Double.valueOf(holder.price1.getText().toString());
//				double sumPrice = price*sum;
//				holder.price.setText("¥"+sumPrice);
//				double mAcSumPrice = Double.valueOf(mActivity.sumPrice.getText().toString());
//				double mAcZePrice = Double.valueOf(mActivity.zePrice.getText().toString());
//				
//				mActivity.sumPrice.setText("合计：¥"+(mAcSumPrice+sumPrice));
//				mActivity.zePrice.setText("");
			}
		});
		holder.reduction.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				try {
					int sum = Integer.valueOf(holder.sumValue.getText().toString());
					if(sum != 1){
						sum--;
						holder.sumValue.setText(sum+"");
						model.setBuyCount((model.getBuyCount()-1));
						mActivity.dbUtils.update(model);
						mActivity.initData();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		
	}

}
